//
//  ZZStatusOriginalView.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/16.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit
import SnapKit

class ZZStatusOriginalView: UIView {

    var bottomConstraints : Constraint?
    
    // MARK: 重写模型的set
    var status: ZZStatus? {
        didSet{
            iconImage.sd_setImageWithURL(status?.user?.headImageURl, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_type_Image
            timeLabel.text = "11:11"
            sourceLabel.text = "来自:iPhone7 plus(5G)"
            contentLabel.text = status?.text
        
            //移除约束
            self.bottomConstraints?.uninstall()
            
            if let picurls = status?.imageURLs where picurls.count > 0 {
                
                //显示配图
                pictureView.hidden = false
                //给配图视图设置数据源
                pictureView.imageURLs = status?.imageURLs
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }else { // 没有分配图
                //隐藏配图
                pictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    // MARK: 重构方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setUpUi() {
        
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(verifiedIconView)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //约束
        //设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.height.equalTo(StatusCellIconWitdth)
            make.width.equalTo(StatusCellIconWitdth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        
        //
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        iconImage.layer.cornerRadius = 17
        iconImage.layer.masksToBounds = true
    }
    
    // MARK: 懒加载控件
    //间隔
    private lazy var sepView: UIView = UIView()
    //头像
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //名字
    private lazy var nameLabel: UILabel = UILabel(title: "xxx", size: 14, color: UIColor.darkGrayColor(), alphaNum: nil)
    //认证图标
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor(), alphaNum: nil)
    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "来自:iPhone7 plus(5G)", size: 10, color: UIColor.lightGrayColor(), alphaNum: nil)
    
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "大牛大牛大牛大牛大牛大牛大牛大牛大牛大牛大牛大牛大牛大牛牛大牛大牛大牛大牛大牛大牛大牛大牛大牛大", size: 14, color: UIColor.lightGrayColor(), alphaNum: nil, margin: StatusCellMargin)
    
    private lazy var pictureView: ZZStatusPictureView = ZZStatusPictureView()
}



